/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.model;

import l1j.server.Thread.GeneralThreadPool;
import l1j.server.echo.ClientThread;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_Paralysis;
import l1j.server.server.templates.L1EtcItem;

// Referenced classes of package l1j.server.server.model:
// L1ItemDelay

public class L1ItemDelay {

/*	private static double startTime;
	private static double endTime;
	private static double totTime;*/

	/**
	 * 建立物件使用延遲計時器
	 * @author daien
	 *
	 */
	static class ItemDelayTimer implements Runnable {
		private final int _delayId;
		private final L1Character _cha;

		public ItemDelayTimer(final L1Character cha, final int id, final int time) {
			this._cha = cha;
			this._delayId = id;
		}


		@Override
		public void run() {
			this.stopDelayTimer(this._delayId);
		}

		/**
		 * 停止該物件使用延遲
		 * @param delayId
		 */
		public void stopDelayTimer(final int delayId) {
			this._cha.removeItemDelay(delayId);
			/*	final L1PcInstance pc = (L1PcInstance)_cha;
			if(pc.isGm()){
			//把要測試的程式放到endTime前面
	        endTime = System.currentTimeMillis();
	      //取得程式結束的時間
	        totTime = endTime - startTime;
	        //計算經過多久
	        System.out.println("Using Time: " + totTime/1000+" sec");
	        System.out.println("Using Time: " + totTime+" ms");
			}*/
		}
	}
	static class TeleportUnlockTimer implements Runnable {
		private final L1PcInstance _pc;

		public TeleportUnlockTimer(L1PcInstance pc) {
			this._pc = pc;
		}

		@Override
		public void run() {
			this._pc.sendPackets(new S_Paralysis(S_Paralysis.TYPE_TELEPORT_UNLOCK,true));
		}
	}

	/**
	 * 建立物件使用延遲
	 * @param pc 執行人物
	 * @param item 物件
	 */
	public static void onItemUse(final ClientThread client, final L1ItemInstance item) {

		int delayId = 0;
		int delayTime = 0;

		final L1PcInstance pc = client.getActiveChar();
		if(pc == null){
			return;
		}
		/*if(pc.isGm()){
	        startTime = System.currentTimeMillis();
		}*/
		if (item.getItem().getType2() == 0) {
			// 種別：その他のアイテム
			delayId = ((L1EtcItem) item.getItem()).get_delayid();
			delayTime = ((L1EtcItem) item.getItem()).get_delaytime();
		} else if (item.getItem().getType2() == 1) {
			// 種別：武器
			return;
		} else if (item.getItem().getType2() == 2) {
			// 種別：防具
			/**隱身斗篷 延遲*/
			if (item.getItem().getItemId() == 20077
					|| item.getItem().getItemId() == 20062
					|| item.getItem().getItemId() == 120077) {
				// インビジビリティクローク、バルログブラッディクローク
				if (item.isEquipped() && !pc.isInvisble()) {
					pc.beginInvisTimer();
				}
			} else {
				return;
			}
		}

		if (delayId != 0 &&delayTime != 0) {
		final ItemDelayTimer timer = new ItemDelayTimer(pc, delayId, delayTime);

		pc.addItemDelay(delayId, timer);
		GeneralThreadPool.getInstance().schedule(timer, delayTime);
		}
	}

	public static void teleportUnlock(L1PcInstance pc, L1ItemInstance item) {
		final int delayTime = ((L1EtcItem) item.getItem()).get_delaytime();
		final TeleportUnlockTimer timer = new TeleportUnlockTimer(pc);
		GeneralThreadPool.getInstance().schedule(timer, delayTime);
	}
	private L1ItemDelay() {
	}
}
